<template>
  <el-dialog
    title="修改商品分类"
    :visible.sync="EditCateVisible"
    width="50%"
    @close="editCateClose"
  >
    <el-form
      :model="editCateData"
      :rules="editCateRules"
      ref="editCateRef"
      label-width="80px"
    >
      <el-form-item label="分类名称" prop="cat_name" size="small">
        <el-input v-model="editCateData.cat_name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="EditCateVisible = false" size="small">取 消</el-button>
      <el-button type="primary" @click="submitEditForm" size="small">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["editCateData"],
  data() {
    return {
      EditCateVisible: false,
      editCateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    // 显示弹出框
    showDialog() {
      this.EditCateVisible = true;
    },
    // 监听新增用户的关闭事件重置表单
    editCateClose() {
      this.$refs.editCateRef.resetFields();
      this.EditCateVisible = false;
    },
    submitEditForm() {
      this.$refs.editCateRef.validate((valid) => {
        if (valid) {
          this.$axios
            .put("categories/" + this.editCateData.cat_id, {
              cat_name: this.editCateData.cat_name,
            })
            .then((res) => {
              if (res.data.meta.status == 200) {
                this.$message.success("修改商品分类成功");
                this.EditCateVisible=false
                this.$emit('refreshMethod',true)
              } else {
                this.$message.error("修改商品分类名称失败");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          this.$message.error("预验证失败");
          return;
        }
      });
    },
  },
};
</script>

<style scoped>
</style>